package com.lfc.mymodule

import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.StrictMode
import android.util.Log
import androidx.multidex.MultiDex
import androidx.multidex.MultiDexApplication
import cn.jpush.android.api.JPushInterface
import com.arialyy.aria.core.Aria
import com.tamsiree.rxkit.RxDeviceTool
import com.tamsiree.rxkit.RxTool
import com.tencent.bugly.crashreport.CrashReport
import kotlin.properties.Delegates

/**
 * Created by LFC
on 2021/1/8.
 */
class MyApp : MultiDexApplication() {
    companion object {
        val TAG = "lfc"

        var context: Context by Delegates.notNull()
            private set

        lateinit var instance: MyApp


    }

//    private var appDB: AppDataBase? = null
//    public fun getAppDB(): AppDataBase? {
//        return appDB
//    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        context = applicationContext
        initConfig()
//        DisplayManager.init(this)
        registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks)
        initBugly()

        //极光初始化,调试模式必须在init之前,发布时应该设置为false,修改APPKEY在manifest中
        JPushInterface.setDebugMode(true)
        JPushInterface.init(this)
        Aria.download(this).register()
    }

    private fun initBugly() {

      /*  val strategy = CrashReport.UserStrategy(this)
        strategy.appChannel = "myChannel";  //设置渠道
        strategy.appVersion = RxDeviceTool.getAppVersionName(this);      //App的版本
        strategy.appPackageName = RxDeviceTool.appPackageName;  //App的包名
        strategy.appReportDelay = 10000;   //  延时 初始化 改为10s
        CrashReport.initCrashReport(this, "cd976863b2", Const.ISDebug, strategy);*/

    }

    private fun initConfig() {
        RxTool.init(this)
      /*  CrashManager.install(this) //异常 捕获 初始化
        NetStateReceiver.registerNetworkStateReceiver(this)

        if (BuildConfig.ISRelease) {
//        if (true) {
            Const.ISDebug = false
            LgU.LEVEL = LgU.DEBUG
        } else {
            Const.ISDebug = true
            LgU.LEVEL = LgU.VERBOSE
        }
        /////TODO  最简单解决  ----7.0----先测试----（已解决）///方法可行
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val builder = StrictMode.VmPolicy.Builder()
            StrictMode.setVmPolicy(builder.build())
        }
        appDB = AppDataBase.getInstance()*/
    }

    override fun attachBaseContext(base: Context) {
//        super.attachBaseContext(MultiLanguageUtils.attachBaseContext(base))
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    private val mActivityLifecycleCallbacks = object : ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            Log.d(TAG, "onCreated: " + activity.componentName.className)
        }

        override fun onActivityStarted(activity: Activity) {
            Log.d(TAG, "onStart: " + activity.componentName.className)

        }

        override fun onActivityResumed(activity: Activity) {

        }

        override fun onActivityPaused(activity: Activity) {

        }

        override fun onActivityStopped(activity: Activity) {

        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

        }

        override fun onActivityDestroyed(activity: Activity) {
            Log.d(TAG, "onDestroy: " + activity.componentName.className)
        }
    }
}